<?php
// +----------------------------------------------------------------------
// | Tplay [ WE ONLY DO WHAT IS NECESSARY ]
// +----------------------------------------------------------------------
// | Copyright (c) 2017 http://tplay.pengyichen.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 听雨 < 389625819@qq.com >
// +----------------------------------------------------------------------


namespace app\admin\controller;

use \think\Db;
use app\admin\controller\Permissions;
class Shequ extends Permissions
{
    /**
     * 
     * @return [type] [description]
     */
    public function index()
    {
        $t = Db::name('shequ')->select();
		$shequ=$this->menulist($t);
        $this->assign('shequ',$shequ);
		// $this->assign('page',$lawyer->render());
        // var_dump($news);
        return $this->fetch();
    }
	public function menulist($menu,$id=0,$level=0){
        
        static $menus = array();
        $size = count($menus)-1;
        foreach ($menu as $value) {
            if ($value['pid']==$id) {
                $value['level'] = $level+1;
                if($level == 0)
                {
                    $value['str'] = str_repeat('',$value['level']);
                    $menus[] = $value;
                }
                elseif($level == 2)
                {
                    $value['str'] = '&emsp;&emsp;&emsp;&emsp;'.'└ ';
                    $menus[$size]['list'][] = $value;
                }
                elseif($level == 3)
                {
                    $value['str'] = '&emsp;&emsp;&emsp;&emsp;&emsp;&emsp;'.'└ ';
                    $menus[$size]['list'][] = $value;
                }
                else
                {
                    $value['str'] = '&emsp;&emsp;'.'└ ';
                    $menus[$size]['list'][] = $value;
                }
                
                $this->menulist($menu,$value['id'],$value['level']);
            }
        }
        return $menus;
    }


    /**
     * 添加新闻
     */
    public function add(){
        //判断是不是操作数据库  
        if($this->request->isPost()){

            $post=$this->request->Post();
            //验证  唯一规则： 表名，字段名，排除主键值，主键名
            $id=$this->request->Post("id");
			if($post['pid']==""){
				$pid=0;
			}else{
				$pid=$post['pid'];
			}
            $data=[
                    'name'=>$post['name'],
                    'pid'=>$post['pid'],
                ];
            if($id==null){       
                $id=Db::name('shequ')->insert($data);
                if($id==false) return $this->error('保存失败');   
            }else{
                $data1=[
                    'name'=>$post['name'],
                    'pid'=>$pid,
                ];
				
                $res = Db::name('shequ')->where('id',$id)->update($data1);
                if($res==false) return $this->error('修改失败');     
            }
            return $this->success("保存成功",'admin/shequ/index');
        }elseif($this->request->has('id')){
            //获取对应数据id
            $id=$this->request->param("id");
            $lawyer=Db::name("lawyer")->where("id",$id)->find();
            // var_dump($friendly);
            $this->assign("lawyer",$lawyer);
            return $this->fetch();
        }else{
			$shequ = Db::name("shequ")->where("pid","0")->order("id","desc")->select();
			$this->assign("shequ",$shequ);
            return $this->fetch();
        }
    }


     /**
     * 新闻删除
     * @return [type] [description]
     */
    public function delete()
    {
        if($this->request->isAjax()) {
            $id = $this->request->has('id') ? $this->request->param('id', 0, 'intval') : 0;
			$info = Db::name("shequ")->where("id",$id)->find();
			if($info['pid']!=0){
				$res = Db::name('shequ')->where('id',$id)->delete();
				if($res){
						return $this->success("删除成功","shequ/index");
					}else{
						return $this->error("删除失败");
					}
			}else{
				$r = Db::name("shequ")->where("pid",$id)->find();
				if(empty($r)){
					$res = Db::name('shequ')->where('id',$id)->delete();
					if($res){
						return $this->success("删除成功","shequ/index");
					}else{
						return $this->error("删除失败");
					}
				}else{
					return $this->error("存在子类型,不可删除");
				}
			}
           
        }
    }
	
	 /**
     * 修改
     * @return [type] [description]
     */
    public function update()
    {
		if(Request()->IsPost()){
			$res = Db::name("shequ")->where("id",Request()->param("id"))->update(["name"=>Request()->param("name")]);
			if($res){
				return $this->success("修改成功","shequ/index");
			}else{
				return $this->error("修改失败");
			}
		}else{
			 $type = Db::name("shequ")->where("id",Request()->param("id"))->find();
			 $this->assign("type",$type);
			 return $this->fetch();
		}
      
    }
	
}
